﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Lswweb.Model;
using Lswweb.IDAL;
using Lswweb.Common;

namespace Lswweb.SqlServerDAL
{
    public class ProductWholesaleRuleDAL : IProductWholesaleRuleDAL
    {
        #region 字段、构造方法、数据映射

        private Builder<ProductWholesaleRuleInfo> builder = new Builder<ProductWholesaleRuleInfo>("ProductWholsaleRule", "Id");

        public ProductWholesaleRuleDAL()
        {
            this.builder.CreateModel += new ReaderBind<ProductWholesaleRuleInfo>(builder_CreateModel);
            this.builder.GetParameters += new ParametersBuilder<ProductWholesaleRuleInfo>(builder_GetParameters);
        }

        IDataParameter[] builder_GetParameters(ProductWholesaleRuleInfo t)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("@Id",t.Id),
                DataAccess.CreateParameter("@MinNumber",t.MinNumber),
                DataAccess.CreateParameter("@MaxNumber",t.MaxNumber),
                DataAccess.CreateParameter("@PriceRate",t.PriceRate)};
        }

        ProductWholesaleRuleInfo builder_CreateModel(Hashtable reader)
        {
            ProductWholesaleRuleInfo model = new ProductWholesaleRuleInfo();

            model.Id = reader.Get<int>("Id");
            model.MinNumber = reader.Get<Int32>("MinNumber");
            model.MaxNumber = reader.Get<Int32>("MaxNumber");
            model.PriceRate = reader.Get<Decimal>("PriceRate");

            return model;
        }

        #endregion

        #region IProductWholesaleRule

        public ProductWholesaleRuleInfo FindById(int id)
        {
            return this.builder.GetModel(id);
        }
        public ProductWholesaleRuleInfo FindByQuantity(int number)
        {
            return this.builder.GetModel(
                "ProductWholsaleRule",
                string.Empty,
                "MinNumber<=@Number and MaxNumber>=@Number",
                string.Empty,
                DataAccess.CreateParameter("@Number", number));
        }

        public IList<ProductWholesaleRuleInfo> FindAll()
        {
            int total;
            return this.builder.GetModels(string.Empty, string.Empty, "order by MinNumber asc", 1, 9999, out total, null);
        }

        public void Add(ProductWholesaleRuleInfo rule)
        {
            this.builder.Insert(
               rule,
               "MinNumber",
               "MaxNumber",
               "PriceRate");
        }

        public void Update(ProductWholesaleRuleInfo rule)
        {
            this.builder.Update(
                rule,
                "Id=@Id",
                "MinNumber",
                "MaxNumber",
                "PriceRate");
        }

        public void Delete(int id)
        {
            this.builder.Delete(id);
        }

        #endregion
    }
}
